﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using System.Security.AccessControl;
using CSBackup.Src.Override;

namespace CSBackup.UI
{
    public partial class frmAutoBkp : Form
    {
        private int _interval; //周期性备份的间隔时间
        //private string _startTime, _endTime;  //任务起始和结束时间
        private int _repeatCount; //任务重复次数， -1则为无限次
        //private ProjectInfo _prjInfo;

        public frmAutoBkp()
        {
            InitializeComponent();
            
        }

        private void frmAutoBkp_Load(object sender, EventArgs e)
        {
            //默认使用一次性备份，周期性设置和自定义周期部分不可用
            rbOneTime.Checked = true;
            gbPrdSet.Enabled = false;
            gbCustom.Enabled = false;

            //设置参数的默认值
            this._interval = 1;//默认一分钟
            //this._startTime = DateTime.Now.ToLongTimeString();
            //this._endTime = "2099/4/4";
            this._repeatCount = -1;
            return;
        }

        private void rbPeriodical_CheckedChanged(object sender, EventArgs e)
        {
            //选择了“周期性”方式，则“周期性设置”部分可用，“自定义周期”部分不可用
            if (rbPeriodical.Checked)
            {
                gbPrdSet.Enabled = true;
                rbEveryDay.Select();
                this._interval = 1440;//一天的分钟数
                this._repeatCount = -1;
            }
            else
            {
                gbPrdSet.Enabled = false;
            }
            return;
        }

        private void rbCustom_CheckedChanged(object sender, EventArgs e)
        {
            //选择了“自定义”方式，则“自定义周期”部分可用，“周期性设置”部分不可用
            if (rbCustom.Checked)
            {
                gbCustom.Enabled = true;
                this._repeatCount = -1;
            }
            else
            {
                gbCustom.Enabled = false;
            }
            return;
        }

        private void rbOneTime_CheckedChanged(object sender, EventArgs e)
        {
            gbPrdSet.Enabled = false;
            gbCustom.Enabled = false;
            this._repeatCount = 1;
            return;
        }

        private void btnABOk_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("software").OpenSubKey("YunGe", true);
            if (key == null)
            {
                key = Registry.LocalMachine.OpenSubKey("software", true).CreateSubKey("YunGe");
            }
            if (this.txtTaskName.Text == "")
            {
                MessageBox.Show("请输入任务名称");
                return;
            }
            if (key.OpenSubKey(this.txtTaskName.Text) != null)
            {
                MessageBox.Show("该名称已被使用");
                return;
            }
            if (!this.txtPrjPath.Text.EndsWith(".bakprj"))
            {
                MessageBox.Show("请选择正确的工程文件");
                return;
            }

            string newName = this.txtTaskName.Text;
            key.CreateSubKey(newName);
            key = key.OpenSubKey(newName, true);
            key.SetValue("interval", this._interval.ToString());
            DateTime startTime = dtpAutoBkp.Value;
            startTime.AddHours((int)nudHour.Value);
            startTime.AddMinutes((int)nudMin.Value);
            key.SetValue("startTime", startTime.ToString());
            key.SetValue("endTime", "2099/4/4");
            key.SetValue("repeatCount", this._repeatCount.ToString());
            key.SetValue("lastBackup", DateTime.Now.ToString());
            key.SetValue("prjPath", this.txtPrjPath.Text);
            key.Close();

            ////安装服务，重复安装不影响， 可以把这部分放到安装包里
            //string path = System.Environment.CurrentDirectory + "\\BackupService.exe";
            //this.InstallService(path, "BackupService");

            MessageBox.Show("设定成功！");
            this.Close();
            //先做一次全备份
            //BackupExport.BackUp(this._prjInfo.BkpPrjFile, this._prjInfo.CurrentBackup.FileList.ToArray(), 
                                          //this._prjInfo.CurrentBackup.FileList.Count, BackupInfo.BACKUP_TYPE_FULL, null, "Data");
        }

        private void InstallService(string path, string name)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine("sc create " + name + " binPath= " + path + " start= auto");
            p.StandardInput.WriteLine("sc start " + name);
        }

        private void rbEveryDay_CheckedChanged(object sender, EventArgs e)
        {
            this._interval = 1440;//一天的分钟数
        }

        private void rbEveryWeek_CheckedChanged(object sender, EventArgs e)
        {
            this._interval = 10080;//一周的分钟数
        }

        private void rbEveryMonth_CheckedChanged(object sender, EventArgs e)
        {
            this._interval = 40320;//一个月的分钟数
        }

        private void rbMinute_CheckedChanged(object sender, EventArgs e)
        {
            this._interval = (int)nudCustomer.Value;
        }

        private void rbHour_CheckedChanged(object sender, EventArgs e)
        {
            this._interval = (int)nudCustomer.Value * 60;
        }

        private void rbDay_CheckedChanged(object sender, EventArgs e)
        {
            this._interval = (int)nudCustomer.Value * 60 * 24;
        }

        private void txtCustomer_TextChanged(object sender, EventArgs e)
        {

        }

        private void dtpAutoBkp_ValueChanged(object sender, EventArgs e)
        {
            //this._startTime = dtpAutoBkp.Value.ToLongTimeString();
        }

        private void btnChoosePrj_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "工程文件(*.bakprj)|*.bakprj";

            DialogResult result = open.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.txtPrjPath.Text = open.FileName;
            }
        }

        private void btnABNo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtHour_TextChanged(object sender, EventArgs e)
        {

        }

        private void nudHour_ValueChanged(object sender, EventArgs e)
        {
            
        }
    }
}
